﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;
using System.Web.Caching;
using System.Collections;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for Per_ActionsRoles
    /// </summary>
    public class TblOptionManager : TblOptionManagerBase
    {
        public TblOptionManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        static string cachekey = "SystemOption";
        public static TblOptionManager CreateInstant()
        {
            return new TblOptionManager();
        }
        public string GetConfig(string Key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[Key];
        }
        public string GetDomainName()
        {
            return System.Configuration.ConfigurationManager.AppSettings["DomainName"];
        }
        public string GetOption(string Key)
        {
            if (HttpRuntime.Cache[cachekey] == null)
            {
                CacheOption();
            }
            Hashtable ht = (Hashtable)HttpRuntime.Cache[cachekey];
            if (ht[Key] != null)
                return ht[Key].ToString();
            return "";
        }
        void CacheOption()
        {
            Hashtable ht = LoadOption();
            HttpRuntime.Cache.Insert(cachekey, ht, null, DateTime.UtcNow.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private Hashtable LoadOption()
        {
            Hashtable resource = new Hashtable();
            List<TblOptionEntity> ls = SelectAll();
            if (ls != null && ls.Count > 0)
            {
                for (int i = 0; i < ls.Count; i++)
                {
                    resource.Add(ls[i].Name, ls[i].Value);
                }
            }
            return resource;
        }
        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        {
            if (HttpRuntime.Cache[cachekey] == null)
            {
                CacheOption();
            }
        }
        public object OptionList(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                List<TblOptionEntity> ls = TblOptionManager.CreateInstant().SelectAll();

                //Return result to jTable
                return new { Result = "OK", Records = ls, TotalRecordCount = ls.Count() };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }
        public  object CreateOption(TblOptionEntity record)
        {
            try
            {
                TblOptionManager.CreateInstant().InsertUpdate(record);
                return new { Result = "OK", Record = record };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }
    }
}
